/**
 * Copyright (C) 2009 Ignasi Fosch Alonso
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 
 * details.
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * You can reach me by mail at natx@y10k.ws
 * You can reach him by paper mail at:
 * C. LLEIDA, 41-43, 3r 1a
 * El Prat de Llobregat 08820
 * Barcelona, SPAIN
 */
package ws.y10k.goloim.interfaces.swt;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.TableItem;

import ws.y10k.goloim.model.Order;
import ws.y10k.goloim.model.OrderAlreadyExistsException;
import ws.y10k.goloim.model.OrderDoesNotExistException;
import ws.y10k.goloim.model.OrderLine;
import ws.y10k.goloim.model.ProductDoesNotExistException;

/**
 * @author natx
 *
 */
public class SaveOrderSelectionListener implements SelectionListener {
	
	private OrderWindow parent;
	
	public SaveOrderSelectionListener(OrderWindow parent) {
		this.parent = parent;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
	 */
	@Override
	public void widgetDefaultSelected(SelectionEvent arg0) { }

	/* (non-Javadoc)
	 * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
	 */
	@Override
	public void widgetSelected(SelectionEvent arg0) {
		if ( parent.txtCode.getText().length() > 0  && parent.txtCustomer.getText().length() > 0) {
			String code = parent.txtCode.getText();
			String customer = parent.txtCustomer.getText();
			Order newOrder = null;
			if ( parent.existingOrder == null ) newOrder = new Order(code,customer);
			else {
				try {
					newOrder = GoLoIm.orders.getOrder(parent.existingOrder.getCode());
					newOrder.setCustomer(customer);
					newOrder.clearOrderLines();
				} catch (OrderDoesNotExistException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			TableItem[] items = parent.tblOrderLines.getItems();
			for ( int i = 0; i < items.length; i++ ) {
				TableItem item = items[i];
				try {
					OrderLine ol = new OrderLine(GoLoIm.products.getProduct(item.getText(0)),Integer.parseInt(item.getText(1)));
					newOrder.add(ol);					
				} catch (NumberFormatException e) {
					MessageDialog.openError(parent.getShell(), "Format error in qty field", "Qty field must be a number.");
					return;
				} catch (ProductDoesNotExistException e) {
					MessageDialog.openError(parent.getShell(), "Product reference doesn't exist", "The product you choosed doesn't exists.");
					return;
				}
			}
			if ( parent.existingOrder == null ) {
				try {
					GoLoIm.orders.add(newOrder);
				} catch (OrderAlreadyExistsException e) {
					MessageDialog.openError(parent.getShell(), "Order already exists", "An order like that already exists.");
					return;
				}
			} else {
				GoLoIm.orders.replace(newOrder);
			}
			GoLoIm.tvrOrders.refresh();
			parent.txtCode.setText("");
			parent.txtCustomer.setText("");
			parent.tblOrderLines.clearAll();
			parent.txtCode.setFocus();
		} else {
			MessageDialog.openError(parent.getShell(), "Not enough information", "You must specify the order code and customer.");
		}
	}

}
